<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<!--A simple javascript for validating the name and passwd are null or not-->
<script type="text/javascript">
function checkLogin(){
		if(document.getElementById("username").value=="") {
			document.getElementById("message").innerHTML = "User name must be entered.";
			return false;
		}
		if(document.getElementById("password").value=="") {
			document.getElementById("message").innerHTML = "Password must be entered.";
			return false;
		}
}
</script>
<meta charset="UTF-8">
<title>Welcome to the Location and game based mobile language learning platform</title>
</head>
<body>
<%
	// The code here check if the user has cookies or not, if he has then login immediately and add a session for him.
	String message = (String)request.getAttribute("message");
	if(message==null) message = new String("");
	Cookie[]  cookies = request.getCookies();
	String  username = null;
	String  designerID = null;
	if(cookies!=null) {
		for(int i = 0 ; i < cookies.length; ++i) {
			if(cookies[i].getName().equals("username")) username = cookies[i].getValue();
			if(cookies[i].getName().equals("designerID")) designerID = cookies[i].getValue();
		}
	}

  if(username!=null && designerID !=null) {
	  HttpSession s = request.getSession();
      s.setAttribute("username", username);
      s.setAttribute("designerID", designerID);
	  response.sendRedirect("designerDashBoard.jsp");
  }
%>
    <form name="loginForm" action="<%=request.getContextPath()%>/DesignerService" method="POST" onSubmit="return checkLogin();">
    <input type="hidden" name="method" value="login" />
      <table>
        <tr>
          <td>
            User name:
          </td>
          <td>
            <input type="text" name="username" id="username">
          </td>
        </tr>
        <tr>
          <td>
            Password:
          </td>
          <td>
            <input type="password" name="password" id="password">
          </td>
        </tr>
        <tr>
          <td height="27" colspan="2" align="center">
            <input type="checkbox" name="remember">
            Remember Me
          <input type="submit" value="Login"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><a href="register_designer.jsp">Register!!</a>></td>
        </tr>
        <tr>
          <td colspan="2" align="center" id="message"><%=message%></td>
        </tr>
      </table>
    </form>
</html>